<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { queryPage } from '@/api/user'
const searchFrom = reactive({
  name: '',
  gender: ''
})
const formData = ref([])
//分页
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(0)
// 查询
const queryPageData = async () => {
  const res = await queryPage(searchFrom.name, searchFrom.gender, currentPage.value, pageSize.value)
  formData.value = res.data.rows
  total.value = res.data.total
}
// 清空
const clear = () => {
  searchFrom.name = ''
  searchFrom.gender = ''
  queryPageData()
}
// 分页
const background = ref(true)
const disabled = ref(false)

const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`)
  currentPage.value = 1
  pageSize.value = val
  queryPageData()
}
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val
  queryPageData()
}
onMounted(() => {
  queryPageData()
})
</script>
<template>
  <div class="container"></div>
  <!-- 搜索框 -->
  <div>
    <h3 align="center">用户信息表</h3>
    <el-form :inline="true" :model="searchFrom">
      <el-form-item label="姓名">
        <el-input v-model="searchFrom.name" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="searchFrom.gender" placeholder="请选择" style="width: 120px;" clearable>
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryPageData()">查询</el-button>
        <el-button type="default" @click="clear()">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
  <!-- 表格 -->
  <div>
    <br />
    <el-table :data="formData"  >
      <el-table-column type="index" prop="id" label="ID" align="center" />
      <el-table-column prop="name" label="姓名" align="center" />
      <el-table-column prop="gender" label="性别" align="center">
        <template #default="scope">
          {{ scope.row.gender === 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" />
    </el-table>
  </div>
  <!-- 分页 -->
  <div>
    <br />
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10, 15, 20]"
      :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>
<style scoped>
</style>